"""
input is a dic 
output is the key list according to the dic
"""

def sortbyvalue(dic,desc=True):
    """
    @param dic
    @param desc return key in descending order

    @return the key list
    """
    result = []
    keys = dic.keys()
    result.append(keys[0])
    keys.remove(keys[0])
    for k in keys:
        if desc:
            if dic[k] <= dic[result[len(result)-1]]:
                result.append(k)
            elif dic[k] >= dic[result[0]]:
                result.insert(0,k)
            else:
                for i in range(len(result)-1):
                    if dic[k] <= dic[result[i]] and dic[k] > dic[result[i+1]]:
                        result.insert(i,k)
                        break
        else:
            if dic[k] >= dic[result[len(result)-1]]:
                result.append(k)
            elif dic[k] <= dic[result[0]]:
                result.insert(0,k)
            else:
                for i in range(len(result)-1):
                    if dic[k] >= dic[result[i]] and dic[k] < dic[result[i+1]]:
                        result.insert(i,k)
                        break
    return result

def sortbykey(dic,desc=True):
    """
    @param dic
    @param desc return key in descending order

    @return the key list

    problem : key type unknow ,now consider as str
    """
    #raise Exception("handle the type problem")
    result = []
    keys = dic.keys()
    result.append(keys[0])
    keys.remove(keys[0])
    for k in keys:
        if desc:
            if k <= result[len(result)-1]:
                result.append(k)
            elif k >= result[0]:
                result.insert(0,k)
            else:
                for i in range(len(result)-1):
                    if k <= result[i] and k > result[i+1]:
                        result.insert(i,k)
                        break
        else:
            #print k
            #print result
            if k >= result[len(result)-1]:
                result.append(k)
            elif k <= result[0]:
                result.insert(0,k)
            else:
                for i in range(len(result)-1):
                    if k >= result[i] and k < result[i+1]:
                        result.insert(i+1,k)
                        break
    return result

def sortbykey2(dic,desc=True,valuefunc=float):
    """
    @param dic
    @param desc return key in descending order

    @return the key list

    problem : key type unknow ,now consider as str
    """
    #raise Exception("handle the type problem")
    result = []
    keys = dic.keys()
    result.append(keys[0])
    keys.remove(keys[0])
    for k in keys:
        if desc:
            if valuefunc(k) <= valuefunc(result[len(result)-1]):
                result.append(k)
            elif valuefunc(k) >= valuefunc(result[0]):
                result.insert(0,k)
            else:
                for i in range(len(result)-1):
                    if valuefunc(k) <= valuefunc(result[i]) and valuefunc(k) > valuefunc(result[i+1]):
                        result.insert(i,k)
                        break
        else:
            if valuefunc(k) >= valuefunc(result[len(result)-1]):
                result.append(k)
            elif valuefunc(k) <= valuefunc(result[0]):
                result.insert(0,k)
            else:
                for i in range(len(result)-1):
                    if valuefunc(k) >= valuefunc(result[i]) and valuefunc(k) < valuefunc(result[i+1]):
                        result.insert(i+1,k)
                        break
    return result

